package dev.alm.reggietakeoutservice.handler;

import dev.alm.reggietakeoutservice.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        try {
            String message = e.getMessage();
            if (message.equals("token is invalid")) {
                return Result.fail(50008, "token is invalid");
            }
            return Result.fail(50000, message);
        } catch (Exception e1) {
            return Result.fail(50000, "系统错误");
        }

    }
}
